﻿/* 
 * Copyright 2010 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Iesi.Collections.Generic;

namespace Ngilead.Core.Replicator.Clone
{
    class CloneSetReplicator : SetReplicator
    {
        #region Attributes

        /// <summary> Logger channel</summary>
        private static ILog logger = LogManager.GetLogger(typeof(CloneSetReplicator));

        /// <summary>Persistence util class</summary>
        private IPersistenceUtil _persistenceUtil;

        #endregion

        #region Properties

        /// <summary>
        /// The associated persistence util
        /// </summary>
        public IPersistenceUtil PersistenceUtil
        {
            get { return _persistenceUtil; }
            set { _persistenceUtil = value; }
        }

        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public CloneSetReplicator()
        {
        }

        #endregion

        #region Overridden

        /// <summary>
        /// Create a new set for the set argument.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="from"></param>
        /// <returns></returns>
        protected override T CreateToSet<T>(T from)
        {
            if (PersistenceUtil.IsPersistentCollection(from.GetType()) == false)
            {
            //  Not a persistent set : just call the base class
            //
                return base.CreateToSet<T>(from);
            }
            else
            {
            //  Get base set type and create a new basic set
            //
                Type componentType = from.GetType();
                if (componentType.IsGenericType)
                {
                    componentType = componentType.GetGenericArguments()[0];
                    Type baseCollectionType = PersistenceUtil.GetUnderlyingCollectionType(from.GetType());
                    if (baseCollectionType.IsGenericTypeDefinition)
                    {
                        Type genericType = baseCollectionType.MakeGenericType(componentType);
                        return (T)Activator.CreateInstance(genericType);
                    }
                    else
                    {
                        Type genericType = typeof(HashedSet<>).MakeGenericType(componentType);
                        return (T)Activator.CreateInstance(genericType);
                    }

                }
                else
                {
                    return (T)Activator.CreateInstance(PersistenceUtil.GetUnderlyingCollectionType(from.GetType()));
                }
            }
        }

        #endregion
    }
}
